package com.happyscan138.makefun.Fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.happyscan138.makefun.BaseAdapter.saokuText;
import com.happyscan138.makefun.R;
import com.happyscan138.makefun.Saoku;
import com.happyscan138.makefun.Urls.Urls;
import com.happyscan138.makefun.adapter.SaokuTextAdapter;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

import static com.happyscan138.makefun.base.http.client;
import static com.happyscan138.makefun.base.http.params;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2017/1/20.
 */

public class Saoku_Text_Fragment extends Fragment {
    private Context mContext;
    private ListView listViewSaoku_text;
    private List<saokuText> listData;
    private String SaokuText;
    private SaokuTextAdapter adapter;
    private Saoku saoku;
    private Intent intent;
    private CallBackValue callBackValue;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.saoku_text, container, false);
        client=new AsyncHttpClient();
        params=new RequestParams();

        initView(view);
        getAdapterView(view);
        return view;
    }
    public void initView(View view) {
        listViewSaoku_text = (ListView) view.findViewById(R.id.saoku_text_listview);


    }
    public View getAdapterView( View view) {

        params.put("page_count","30");
        params.put("last_index_key","0");
        log.e("params",params.toString());
        RequestHandle handle=client.post(Urls.TextList,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                String jsonStr=response.toString();
                log.e("TextListjsonStr",jsonStr);
                listData=new ArrayList<>();
                try {
                    JSONObject jsonObject=new JSONObject(jsonStr);
                    JSONArray jsonArray=jsonObject.getJSONArray("data");
                    for (int i=0;i<jsonArray.length();i++){
                        JSONObject object=jsonArray.getJSONObject(i);
                        saokuText data=new saokuText();
                       data.text=object.getString("content");
                        listData.add(data);
                    }

                    log.e("listData",listData.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                adapter=new SaokuTextAdapter(getActivity(),listData);
                listViewSaoku_text.setAdapter(adapter);

                adapter.setSaoOnAddClickListener(new SaokuTextAdapter.SaoKuAddClickListener() {
                    @Override
                    public void onItemClick(String positionText) {
                                SaokuText=positionText;
                        intent =new Intent();
                        Bundle  builder=new Bundle();
                        builder.putString("SaokuText",SaokuText);
                        getActivity().setResult(1,getActivity().getIntent().putExtras(builder));
                        callBackValue.SendMessageValue(SaokuText);


                        getActivity().finish();
                    }
                });
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                log.e("SaokuText",responseString);
            }
        });
        return view;
    }
    /**
     * fragment与activity产生关联是  回调这个方法
     */
    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        //当前fragment从activity重写了回调接口  得到接口的实例化对象
        callBackValue =(CallBackValue) getActivity();
    }


    //定义一个回调接口
    public interface CallBackValue{
        public void SendMessageValue(String strValue);
    }
}
